﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Heyu.Utility
{
    public class DataTypeUtility
    {
        /// <summary>
        /// 图片
        /// </summary>
        public static string[] imgExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
        /// <summary>
        /// //压缩包
        /// </summary>
        public static string[] packageExtensions = { ".rar", ".zip" };
        /// <summary>
        /// //flash
        /// </summary>
        public static string[] flashExtensions = { ".swf" };
        /// <summary>
        /// //音频
        /// </summary>
        public static string[] audioExtensions = { ".mp3" };
        /// <summary>
        /// //视频
        /// </summary>
        public static string[] videoExtensions = { ".mp4", ".avi" };
        public static string[] excelExtensions = { ".xls", ".xlsx" };



        [Obsolete]
        public static bool CheckData(string tips, StringType type, string value, out string error)
        {
            return Validate(type, value, tips, out error);
        }

        /// <summary>
        /// 检查数据格式
        /// </summary>
        /// <param name="tips">检测项目</param>
        /// <param name="type">格式要求</param>
        /// <param name="value">值</param>
        /// <param name="error">输出错误</param>
        /// <returns></returns>
        public static bool Validate(StringType type, string value, string tips, out string error)
        {
            error = "";
            bool flag = false;
            //初始化错误提示
            tips += "格式不正确!";

            if (string.IsNullOrEmpty(value))
            {
                return true;
            }

            switch (type)
            {
                case StringType.Email:
                    flag = Regex.IsMatch(value, RegexUtility.EMAIL);
                    break;
                case StringType.Ssdq:
                    if (Regex.IsMatch(value, @"^[0-9]{8}$"))
                    {
                        flag = true;
                    }
                    else
                    {
                        tips += "必须是8位数字!";
                    }
                    break;
                case StringType.Int:

                    if (Regex.IsMatch(value, RegexUtility.INT))
                    {
                        flag = true;
                    }
                    else
                    {
                        tips += "只能输入整数！";
                    }
                    break;
                case StringType.Int8:
                    if (Regex.IsMatch(value, @"^[0-9]{8}$"))
                    {
                        flag = true;
                    }
                    else
                    {
                        tips += "只能输入8位整数！";
                    }
                    break;
                case StringType.Gender:
                    if (value == "0" || value == "1")
                    {
                        flag = true;
                    }
                    else
                    {
                        tips += "性别格式不正确，只能是 0、1！";
                    }
                    break;
                case StringType.DateTime:
                    DateTime _date;
                    if (DateTime.TryParse(value, out _date))
                    //if (Regex.IsMatch(value, @"^((((19|20)\d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|(((19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$"))
                    {
                        flag = true;
                    }
                    break;
                case StringType.Date:
                    if (Regex.IsMatch(value, @"^((((19|20)\d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|(((19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$"))
                    {
                        flag = true;
                    }
                    break;
                case StringType.Time:
                    if (Regex.IsMatch(value, @"^$"))
                    {
                        flag = true;
                    }
                    break;
                case StringType.IDNumber:
                    if (Regex.IsMatch(value, @"^[1-9]{1}[0-9]{16}[0-9xX]$"))
                    {
                        flag = true;
                    }
                    break;
                case StringType.DriverNumber:
                    if (Regex.IsMatch(value, @"^[1-9]{1}[0-9]{16}[0-9xX]$"))
                    {
                        flag = true;
                    }
                    break;

                default:
                    tips += "没有找到格式验证规则！";
                    break;
            }

            error = tips + ("【" + value + "】");

            return flag;
        }

    }
}
